﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace QidorSpider.Components.Util
{
    public class LogHelper
    {
        public static void WriteTaskLog(string taskName, Common.LogType logType, string logMessage)
        {
            string logContent = "";
            string logFile = Common.ApplicationPath + "Log\\" + taskName + "_" + DateTime.Now.ToString("yyyyMMdd") + ".log";

            if(!Directory.Exists(Path.GetDirectoryName(logFile)))
                Directory.CreateDirectory(Path.GetDirectoryName(logFile));

            FileStream myStream = File.Open(logFile, FileMode.Append, FileAccess.Write, FileShare.Write);
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));

            if(logType == Common.LogType.GatherError)
            {
                logContent = taskName + "\t" + DateTime.Now.ToString() + "\t" + "采集错误" + "\t" + logMessage;
                sw.WriteLine(logContent);
            }
            else if(logType == Common.LogType.PublishError)
            {
                logContent = taskName + "\t" + DateTime.Now.ToString() + "\t" + "发布错误" + "\t" + logMessage;
                sw.WriteLine(logMessage);
            }

            sw.Close();
            myStream.Close();
        }


    }
}
